package com.yd.app.service.pc.messageGroupService;

import com.yd.app.mapper.MessageGroupMapper;
import com.yd.app.service.common.BaseService;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.MessageGroupSearchParam;
import com.yd.domain.sys.MessageGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2018/6/5.
 */
@Service
public class MessageGroupService extends BaseService {
    @Autowired
    MessageGroupMapper messageGroupMapper;

    public void addMessageGroup(MessageGroup messageGroup) {
        List<String> userIds = messageGroupMapper.getUserList();
        for(String userId:userIds){
            messageGroup.setUser(userId);
            messageGroupMapper.addMessageGroupUser(messageGroup);
        }
        messageGroupMapper.addMessageGroup(messageGroup);
    }

    public PageModel messageGroupList(MessageGroupSearchParam param, int pageIndex, int pageSize, String userId) {
        PageModel pm=new PageModel();
        List<MessageGroup> returnList=messageGroupMapper.getMessageGroupList(param,getStartRow(pageIndex,pageSize),pageSize);
        int count=0;
        if(pageIndex==1){
            //查询总条数
            count=messageGroupMapper.getMessageGroupListCount(param);
            if(count>0){
                count=count-1;
            }
        }
        pm.setCount(count);
        pm.setList(returnList);
        return  pm;
    }

    @Transactional
    public void deleteMessageGroupById(String messageGroupId) {
        messageGroupMapper.deleteMessageGroupById(messageGroupId);
        messageGroupMapper.deleteMessageGroupRelationById(messageGroupId);
    }

    public void updateMessageGroup(MessageGroup messageGroup) {
        messageGroupMapper.updateMessageGroup(messageGroup);
    }

    public List<MessageGroup> getMessageGroupUserList(String messageGroupId) {
       return messageGroupMapper.getMessageGroupUserList(messageGroupId);
    }
    /**
     * @Author: zy
     * @Desc: 获取群发消息数量
     * @Date:  2018/6/6
     * @Param
     */
    public int getNoReadQF(String userId) {
        int count=messageGroupMapper.getNoReadQF(userId);
        return count;
    }
    /**
     * @Author: zy
     * @Desc: 手机端获取群发消息列表
     * @Date:  2018/6/6
     * @Param
     */
    public PageModel getCusQFMsg(MessageGroupSearchParam param, int pageIndex, int pageSize) {
        PageModel pm=new PageModel();
        List<MessageGroup> returnList=messageGroupMapper.getCusQFMsgList(param,getStartRow(pageIndex,pageSize),pageSize);
        int count=0;
        if(pageIndex==1){
            //查询总条数
            count=messageGroupMapper.getCusQFMsgCount(param);
            if(count>0){
                count=count-1;
            }
        }
        pm.setCount(count);
        pm.setList(returnList);
        return  pm;
    }
    /**
     * @Author: zy
     * @Desc: 手机端删除群发消息
     * @Date:  2018/6/6
     * @Param
     */
    public void deleteQf(String id, String userId) {
        messageGroupMapper.deleteQf(id,userId);
    }

    public void updateRead(String id, String userId) {
        messageGroupMapper.updateRead(id,userId);
    }
}
